package MyThreadPoolExecutor;

import Producer_Consumer.ArrayBlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 38917
 * Date: 2025-03-09
 * Time: 00:50
 */
class MyThreadPoolExecutor{
    private final int capacity = 1000;
    private final ArrayBlockingQueue queue = new ArrayBlockingQueue(capacity);

    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }

    public MyThreadPoolExecutor(int num){
        for (int i = 0; i < num; i++) {
            Thread thread = new Thread(()->{
                while (true) {
                    try {
                        queue.take().run();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
            thread.start();
        }
    }

    public int getCapacity(){
        return capacity;
    }
}

public class TestMyThreadPoolExecutor {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPoolExecutor myThreadPoolExecutor = new MyThreadPoolExecutor(4);
        for (int i = 1; i <= myThreadPoolExecutor.getCapacity(); i++) {
            int n = i;
            myThreadPoolExecutor.submit(()-> System.out.println("执行任务：" + n + " " + Thread.currentThread().getName()));
        }
    }
}
